package com.syerit.modules.worldgreen3d.control.commands
{
	import beherit.fl.loaders.SimpleLoader;
	import beherit.fl.loaders.utils.LoaderEvent;
	
	import br.com.stimuli.loading.BulkErrorEvent;
	import br.com.stimuli.loading.BulkLoader;
	
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.arc90.modular.ModuleSequenceCommand;
	import com.syerit.modules.worldgreen3d.model.ModuleModelLocator;
	
	/**
	 * @version $Revision: $
	 */
	public class LoadDataCommand extends ModuleSequenceCommand
	{  
		private var appModel :ModuleModelLocator = ModuleModelLocator.getInstance();
		private var loader:SimpleLoader;
		
	 	public function LoadDataCommand()
	 	{
	 	
		}    
	  	override public function execute( event : CairngormEvent ) : void
   		{
   			loader = new SimpleLoader("initModuleLoader",appModel.settingsModel.prefix);
			loader.addEventListener(LoaderEvent.DATA_LOADED,onDataLoaded);
			loader.addEventListener(LoaderEvent.PROGRESS,onProgress);
			parseLoadingData();
			loader.start();
			trace("sadjdsakd");
   		}
   		private function onDataLoaded(_e:LoaderEvent):void
   		{
   			updateLoadedDataModel();
   			trace("com.modules.worldgreen3d.control.commands.LoadDataCommand: Data Loaded Succesfully");
   			//var event:ProcessDataEvent = new ProcessDataEvent();
   			//event.dispatch();
   			appModel.viewModel.module.contentLoaded();
   		}
   		private function onProgress(_e:LoaderEvent):void
   		{
   			appModel.viewModel.module.contentProgress(_e.progress);
   		}
   		private function parseLoadingData():void
   		{
   			var loadingItems:XMLList = appModel.loadedDataModel.appData.LOAD.ITEM;
   			for(var i:int=0;i<loadingItems.length();i++)
   			{
   				loader.add(loadingItems[i].@name,loadingItems[i].@src,loadingItems[i].@type);
   				trace("com.modules.worldgreen3d.control.commands.LoadDataCommand: loading: ","id:"+loadingItems[i].@name,"src:"+loadingItems[i].@src,"type:"+loadingItems[i].@type);
   			}
   		}
   		private function loadError(_e:BulkErrorEvent):void
		{
        	trace("#ERROR(com.modules.worldgreen3d.control.commands.LoadModuleDataCommand):",_e.errors, "has failed to load");
		}
		private function updateLoadedDataModel():void
   		{
   			appModel.loadedDataModel.images = loader.loadedImages;
   			appModel.loadedDataModel.xml = loader.loadedXML;
   			appModel.loadedDataModel.sound = loader.loadedSound;
   			appModel.loadedDataModel.swf = loader.loadedSwf;
   			appModel.loadedDataModel.text = loader.loadedText;
   			appModel.loadedDataModel.video = loader.loadedVideo;
   		}
	}
}